<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 会员 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" class="handle-del mr10" @click="addDialog">新增会员</el-button>
            <div class="handle-box">
                <!-- <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button> -->
                <el-input v-model="query.keyword" placeholder="输入会员名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column label="会员头像">
                    <template slot-scope="scope">                        
                        <img :src="scope.row.avatar" @error="tabErrorPicture(scope.row.id)" alt="" class="info-image" v-if="scope.row.avatar" />
                        <img :src="errorProduct" alt="" class="info-image" v-else />
                    </template>
                </el-table-column>
                <el-table-column prop="nickname" label="会员名称"></el-table-column>
                <el-table-column prop="title" label="会员信息"></el-table-column>
                <!-- <el-table-column prop="status" label="状态"></el-table-column> -->
                <el-table-column prop="end_time" label="到期时间"></el-table-column>
                <!-- <el-table-column prop="time" label="更新时间"></el-table-column> -->
                <el-table-column label="操作" width="180" align="center" fixed="right">
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row.id, scope.row)" type="success" size="mini">编辑</el-button>
                        <el-button @click="deleteRow(scope.row.id, scope.$index)" type="danger" size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.page"
                    :page-size="query.rows"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 新增会员弹出框 -->
        <el-dialog title="新增/编辑会员" :visible.sync="showDialog" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="会员类型">
                    <el-select v-model="form.mt_id" clearable placeholder="请选择会员类型">
                        <el-option v-for="item in options" :key="item.id" :label="item.title" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="选择用户">
                    <el-button @click="chooseUser" type="primary" size="small">选择用户</el-button>
                    <div class="choose_user" v-if="form.nickname">
                        <div class="user_avatar">
                            <img :src="form.avatar" alt="" />
                        </div>
                        <div class="user_nickname">{{ form.nickname }}</div>
                    </div>
                </el-form-item>
                <el-form-item label="会员到期时间">
                    <div>
                        <el-date-picker
                            v-model="form.end_time"
                            :disabled="endTime_disable"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            type="datetime"
                            placeholder="选择日期时间"
                        >
                        </el-date-picker>
                    </div>
                    <div style="margin-top: 10px">
                        <el-checkbox v-model="form.foreverMember" @change="chooseForever">永久会员</el-checkbox>
                    </div>
                </el-form-item>
                <!-- <el-form-item label="状态">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="true">启用</el-radio>
                        <el-radio :label="false">禁用</el-radio>
                    </el-radio-group>
                </el-form-item> -->
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 选择用户弹框 -->
        <el-dialog width="36%" title="选择用户" :visible.sync="innerVisible" append-to-body>
            <div class="choice-box">
                <el-input v-model="userQuery.keyword" placeholder="输入用户名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearchCourse">搜索</el-button>
            </div>
            <div class="course_list" style="height: 300px">
                <div
                    @click="choiceCourse(item.uid, item.avatar, item.nickname)"
                    class="course_list_item"
                    v-for="(item, index) in userList"
                    :key="index"
                >
                    <div class="avatar">
                        <img :src="item.avatar" alt="" />
                    </div>
                    <div class="nickname">{{ item.nickname }}</div>
                </div>
            </div>
            <div class="innerPagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="userQuery.page"
                    :page-size="userQuery.rows"
                    :total="userPageTotal"
                    @current-change="userHandlePageChange"
                ></el-pagination>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="exit">关 闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { memberListData, memberSetListData, userListData, addMemberData, delMemberData } from '../../api/index';
export default {
    // name: 'exercise',
    data() {
        return {
            query: {
                page: 1,
                rows: 4,
                keyword: ''
            },
            userQuery: {
                page: 1,
                rows: 9,
                keyword: ''
            },
            form: {
                id: '',
                mt_id: '',
                uid: '',
                avatar: '',
                nickname: '',
                end_time: '',
                foreverMember: ''
            },
            endTime_disable: false,
            tableData: [],
            options: [],
            userList: [],
            multipleSelection: [],
            delList: [],
            showDialog: false,
            innerVisible: false,
            pageTotal: 0,
            userPageTotal: 0,
            errorProduct: require('@/assets/img/avatar.png'),
        };
    },
    created() {
        this.getData();
    },
    methods: {
        tabErrorPicture(id){    // 表格图文封面默认展示图
            let i = this.tableData.findIndex(v => v.id == id);
			this.tableData[i].avatar = this.errorProduct;
        },
        getData() {
            memberListData(this.query).then((res) => {
                this.tableData = res.data.list;
                this.pageTotal = res.data.count;
            });
            memberSetListData().then((res) => {
                this.options = res.data.list;
            });
            userListData(this.userQuery).then((res) => {
                this.userList = res.data.list;
                this.userPageTotal = res.data.count;
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 搜索
        handleSearch() {
            this.$set(this.query);
            this.getData();
        },
        handleSearchCourse() {
            this.$set(this.userQuery);
            this.getData();
        },
        //新增
        addDialog() {
            this.showDialog = true;
            this.form = {};
            this.form.status = '1';
            this.endTime_disable = false;
            this.form.foreverMember = false;
        },
        //编辑
        edit(id, row) {
            this.showDialog = true;
            this.form = row;
            this.form.id = id;
            if (row.end_time == '永久') {
                this.form.end_time = 0;
                this.endTime_disable = true;
                this.form.foreverMember = true;
            } else {
                this.endTime_disable = false;
            }
        },
        //删除
        deleteRow(id, index) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                delMemberData({ id: id }).then((res) => {
                    if (res.code == 1) {
                        this.$message.success('删除成功');
                        this.tableData.splice(index, 1);
                        setTimeout(() => {
                            this.getData();
                            //location.reload();
                        }, 200);
                    }
                });
            });
        },
        chooseUser() {
            this.innerVisible = true;
        },
        choiceCourse(uid, avatar, nickname) {
            this.innerVisible = false;
            this.form.uid = uid;
            this.form.avatar = avatar;
            this.form.nickname = nickname;
        },
        chooseForever(val) {
            if (val == true) {
                this.form.end_time = 0;
                this.endTime_disable = true;
            } else {
                this.endTime_disable = false;
            }
        },
        //取消
        cancel() {
            this.showDialog = false;
        },
        //确定
        save() {
            addMemberData(this.form).then((res) => {
                if (res.code == 1) {
                    this.$message.success('操作成功');
                    this.showDialog = false;
                    setTimeout(() => {
                        this.getData();
                        //location.reload();
                    }, 200);
                }else if(res.code==-2){
                    this.$alert('该用户已经有会员卡了', '消息提示', {confirmButtonText: '确定',});
                }
            });
        },
        exit() {
            this.innerVisible = false;
        },
        // 分页
        handlePageChange(val) {
            this.$set(this.query, 'page', val);
            this.getData();
        },
        userHandlePageChange(val) {
            this.$set(this.userQuery, 'page', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
    float: right;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}

.mr10 {
    margin-right: 10px;
}
.info-image {
    width: 60px;
    height: 60px;
    border-radius: 50%;
}
.user_avatar {
    float: left;
    width: 50px;
    height: 50px;
}
.user_avatar img {
    width: 100%;
    border-radius: 50%;
    overflow: hidden;
}
.user_nickname {
    margin-left: 60px;
    line-height: 50px;
}
.choice-box {
    text-align: center;
    margin-bottom: 20px;
}
.course_list_item {
    float: left;
    width: 180px;
    margin: 10px;
    padding: 5px;
    border: 1px solid #eee;
    cursor: pointer;
    height: 62px;
}
.course_list_item .avatar {
    float: left;
    width: 50px;
    height: 50px;
}
.course_list_item .avatar img {
    width: 100%;
    border-radius: 50%;
}
.course_list_item .nickname {
    margin-left: 60px;
    line-height: 28px;
}
.innerPagination {
    float: left;
}
.dialog-footer {
    display: block;
}
</style>


